package com.xingkeduo.business.order.controller;

import com.xingkeduo.business.order.domain.OrderDomainService;
import com.xingkeduo.business.order.service.OrderService;
import com.xingkeduo.dto.param.OrderModifyDto;
import com.xingkeduo.dto.param.OrderQueryDto;
import com.xingkeduo.dto.response.OrderDetailResponseDto;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.xingkeduo.utils.Constant.Cms_URL_PREFIX;

/**
 * @author chi  2019-02-13 15:10
 **/
@RestController
@RequestMapping(Cms_URL_PREFIX + "/order")
@Slf4j
public class OrderCmsController {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderDomainService orderDomainService;

    /**
     * 分页查询
     *
     * @param orderQueryDto
     * @return
     */
    @GetMapping
    public ResponseEntity get(OrderQueryDto orderQueryDto) {
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderService.selectListPage(orderQueryDto)), HttpStatus.OK);
    }

    /**
     * 根据id获得详情
     *
     * @return
     */
    @GetMapping("/")
    public ResponseEntity get(@PathVariable Integer id, boolean detail) {
        OrderDetailResponseDto orderDetailResponseDto = orderService.queryById(id, detail);
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderDetailResponseDto), HttpStatus.OK);
    }


    /**
     * 根据订单id查询订单详情
     * @param id
     * @param detail
     * @return
     */
    @GetMapping("/query")
    public ResponseEntity queryOrder(int id, boolean detail) {
        OrderDetailResponseDto orderDetailResponseDto = orderService.queryById(id, detail);
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderDetailResponseDto), HttpStatus.OK);
    }


    @GetMapping("/query/{id}")
    public ResponseEntity get11(@PathVariable Integer id, boolean detail) {
        OrderDetailResponseDto orderDetailResponseDto = orderService.queryById(id, detail);
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderDetailResponseDto), HttpStatus.OK);
    }


    /**
     * 根据订单No查询订单详情
     * @param id
     * @param detail
     * @return
     */
    @GetMapping("/query/no")
    public ResponseEntity queryOrderNo(String id, boolean detail) {
        OrderDetailResponseDto orderDetailResponseDto = orderService.queryByOrderNO(id, detail);
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderDetailResponseDto), HttpStatus.OK);
    }




    /**
     * 后台根据id修改订单信息
     *
     * @return
     */
    @PostMapping("/modify")
    public ResponseEntity modify(@RequestBody @Valid OrderModifyDto orderModifyDto) {
        log.info("modify order:{}", orderModifyDto);
        orderDomainService.modify(orderModifyDto);
        return new ResponseEntity<>(ResponseUtils.getSuccess(), HttpStatus.OK);
    }

    /**
     * 后台根据id手动完成订单
     *
     * @return
     */
    @PostMapping("/complete")
    public ResponseEntity complete(@RequestBody @Valid OrderModifyDto orderModifyDto) {
        log.info("complete order:{}", orderModifyDto);
        orderDomainService.complete(orderModifyDto);
        return new ResponseEntity<>(ResponseUtils.getSuccess(), HttpStatus.OK);
    }


    /**
     * 根据id获得详情
     *
     * @return
     */
    @GetMapping("/order/info/{orderNo}")
    public ResponseEntity queryByOrderNO(@PathVariable String orderNo) {
        OrderDetailResponseDto orderDetailResponseDto = orderService.queryByOrderNO(orderNo);
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderDetailResponseDto), HttpStatus.OK);
    }

}



